package com.lord.otter.component.infrastructure.retry.executor;

import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

/**
 * RestTemplate 实现执行器
 * @author lord
 * 2025/9/5 23:30
 */
@RequiredArgsConstructor
public class RestTemplateExecutor implements HttpExecutorFacade {
    private final RestTemplate restTemplate;

    /**
     * 执行请求
     *
     * @param url         地址
     * @param httpMethod  请求方式
     * @param httpHeaders 请求头
     * @param requestBody 请求体
     * @author lord
     * 2025/8/16 23:23
     */
    @Override
    public ResponseEntity<String> execute(
            String url,
            HttpMethod httpMethod,
            HttpHeaders httpHeaders,
            String requestBody
    ) throws Exception {
        HttpEntity<Object> request = new HttpEntity<>(requestBody, httpHeaders);
        return restTemplate.exchange(url, httpMethod, request, String.class);
    }

    /**
     * 执行请求
     *
     * @param url          地址
     * @param httpMethod   请求方式
     * @param httpHeaders  请求头
     * @param requestBody  请求体
     * @param responseType 响应类型
     * @author lord
     * 2025/8/16 23:23
     */
    @Override
    public <T> ResponseEntity<T> execute(
            String url,
            HttpMethod httpMethod,
            HttpHeaders httpHeaders,
            String requestBody,
            Class<T> responseType
    ) throws Exception {
        HttpEntity<Object> request = new HttpEntity<>(requestBody, httpHeaders);
        return restTemplate.exchange(url, httpMethod, request, responseType);
    }
}
